package nc.ui.erm.util;

import java.util.ArrayList;
import java.util.List;
import nc.ui.bd.def.DefquoteQueryUtil;
import nc.ui.bd.def.IDefShowWhenRef;
import nc.ui.er.util.BXUiUtil;
import nc.ui.pub.ClientEnvironment;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pub.bill.BillData;
import nc.ui.trade.bill.DefaultDefShowStrategy;
import nc.ui.trade.bill.IDefShowStrategy;
import nc.ui.trade.business.HYPubBO_Client;
import nc.vo.bd.CorpVO;
import nc.vo.bd.def.DefVO;

public class CardDefShowUtil
  implements IDefShowWhenRef
{
  private BillCardPanel m_cardpanel;
  private IDefShowStrategy _showStrategy;

  public CardDefShowUtil(BillCardPanel cardpanel)
  {
    this.m_cardpanel = cardpanel;
    this._showStrategy = new DefaultDefShowStrategy();
  }

  public CardDefShowUtil(BillCardPanel cardpanel, IDefShowStrategy showStrategy)
  {
    this.m_cardpanel = cardpanel;
    this._showStrategy = showStrategy;
  }

  public void showDefWhenRef(String[] strDefObjs, String[] strPrefix, boolean isHead)
    throws Exception
  {
    if (strDefObjs.length != strPrefix.length) {
      return;
    }
    ArrayList billDef = HYPubBO_Client.queryDefVO(strDefObjs, BXUiUtil.getLogin_unit());

    if ((billDef == null) || (billDef.isEmpty())) {
      return;
    }

    for (int i = 0; i < strPrefix.length; i++) {
      DefVO[] defVOs = (DefVO[])(DefVO[])billDef.get(i);

      if (defVOs == null) {
        continue;
      }
      DefVO[] defVOsnew = new DefVO[defVOs.length];

      for (int j = 0; j < defVOs.length; j++) {
        if (defVOs[j] != null) {
          String fieldName = defVOs[j].getFieldName();
          String ids = fieldName.substring(fieldName.indexOf(strPrefix[i]) + strPrefix[i].length());
          defVOsnew[(new java.lang.Integer(ids).intValue() - 1)] = defVOs[j];
        }
      }

      setDefVisuabled(defVOsnew, strPrefix[i], isHead);
    }
    this.m_cardpanel.setBillData(this.m_cardpanel.getBillData());
  }

  public void showDefWhenRef(String[] strDefObjs, String[] strPrefix, boolean[] isHeads) throws Exception {
    if ((strDefObjs.length != strPrefix.length) || (strDefObjs.length != isHeads.length)) {
      return;
    }
    String pk_corp = ClientEnvironment.getInstance().getCorporation().getPrimaryKey();
    List billDef = DefquoteQueryUtil.getInstance().queryDefusedVO(strDefObjs, pk_corp);

    if ((billDef == null) || (billDef.isEmpty())) {
      return;
    }

    for (int i = 0; i < strPrefix.length; i++) {
      DefVO[] defVOs = (DefVO[])(DefVO[])billDef.get(i);
      DefVO[] defVOsnew = new DefVO[defVOs.length];
      for (int j = 0; j < defVOs.length; j++) {
        if (defVOs[j] != null) {
          String fieldName = defVOs[j].getFieldName();
          String ids = fieldName.substring(fieldName.indexOf(strPrefix[i]) + strPrefix[i].length());
          defVOsnew[(new java.lang.Integer(ids).intValue() - 1)] = defVOs[j];
        }
      }
      setDefVisuabled(defVOsnew, strPrefix[i], isHeads[i]);
    }

    this.m_cardpanel.setBillData(this.m_cardpanel.getBillData());
  }

  private void setDefVisuabled(DefVO[] vos, String prefix, boolean isHead)
  {
    if ((vos != null) && (vos.length != 0))
      if (isHead) {
        this.m_cardpanel.getBillData().updateItemByDef(vos, prefix, isHead);
        SetDefVisuabled.getInstance(this._showStrategy).setDefVisuabled(this.m_cardpanel.getBillData(), "", vos, prefix, isHead);
      } else {
        String[] bodyTableCodes = this.m_cardpanel.getBillData().getBodyTableCodes();
        if(null != bodyTableCodes && 0 < bodyTableCodes.length){
	        for (String tab : bodyTableCodes) {
	          if ((!tab.equals("er_finitem")) && (!tab.equals("er_bxcontrast"))) {
	            this.m_cardpanel.getBillData().updateItemByDef(tab, vos, prefix, isHead);
	          }
	          SetDefVisuabled.getInstance(this._showStrategy).setDefVisuabled(this.m_cardpanel.getBillData(), tab, vos, prefix, isHead);
	        }
      	}
      }
  }
}